<?php

namespace My\Controller;

use Utuo\BackgroundController;

class WechatActivityRedPaperController extends BackgroundController {
	// init
	public function _initialize() {
		$this->setControllerTitle('全民大富翁');
		$this->setActionTitle(array (
				'index' => '活动管理',
				'add' => '添加活动',
				'recycle' => '回收站',
				'modify' => '修改活动',
				'delete' => '删除活动' ,
				'host' => '发起活动情况',
				'play' => '抽奖明细',
				'register' => '中奖登记名单'
		));
		
		$this->setActionNav('index,add,recycle');
	}
	
	// getList
	public function getLists($is_deleted) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$d = D('WechatActivityRedPaper');
		$condition = array (
				'cid' => $this->aid,
				'is_deleted' => $is_deleted 
		);
		$list = $d->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		$count = $d->where($condition)->count();
		foreach ($list as &$v) {
// 			$v['activityUrl'] = $this->getUrl($this->aid, $v['id']);
		    $v['activityUrl'] = uu($this->aid,'Home','RedPaper','index',array('cid'=>$this->aid,'aid'=>$v['id']),true);
		}
		$this->assign('itemList', $list);
		$this->showPage($count, $pagesize);
		$this->display();
	}
	
	// 查看发起活动情况
	public function hostAction() {
		$id = I('id',0,'intval');
		$m = M('WechatActivityRedPaperHost h');
		$m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=h.uid');
		$m->where('h.cid=%d AND h.aid=%d', $this->aid,$id);
		$m->field('h.*, u.nickname,u.headimgurl,u.province,u.city');
		$m->order('h.money DESC');
		$mCount = clone $m;
		$count = $mCount->count();
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$lists = $m->limit($pagesize)->page($page)->select();
		$playCount = M('WechatActivityUser')->where(array('cid'=>$this->aid,'aid'=>$id,'tag'=>'rich_paper'))->count();
		$this->assign('hostCount',$count);
		$this->assign('playCount',$playCount);
		$this->assign('lists', $lists);
		$this->showPage($count, $pagesize);
		$this->display();
	}
	
	// 查看参与人员情况
	public function playAction() {
		$id = I('id',0,'intval');
		$aid = I('aid',0,'intval');
		$m = M('WechatActivityRedPaperPlay p');
		$m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=p.uid');
		$m->where('p.cid=%d AND p.aid=%d AND p.hid=%d', $this->aid,$aid,$id);
		$m->field('p.*, u.nickname,u.headimgurl,u.province,u.city');
		$mCount = clone $m;
		$count = $mCount->count();
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$lists = $m->limit($pagesize)->page($page)->select();
		$this->assign('lists', $lists);
		$this->showPage($count, $pagesize);
		$this->display();
	}
	
	// 查看登记中奖名单情况
	public function registerAction() {
		$id = I('id',0,'intval');
		$m = M('WechatActivityRedPaperRegister r');
		$m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=r.uid');
		$m->join('LEFT JOIN __WECHAT_ACTIVITY_RED_PAPER_HOST__ h ON h.id=r.hid');
		$m->where('r.cid=%d AND r.aid=%d', $this->aid,$id);
		$m->field('r.*, u.nickname,u.headimgurl,u.province,u.city,h.money,h.create_time');
		$lists = $m->select();
		$this->assign('lists', $lists);
		$this->display();
	}
	
	// 获取前台活动URL
	protected function getUrl($cid, $aid) {
		$url = U('Home/RedPaper/index', array (
				'cid' => $cid,
				'aid' => $aid 
		), true, true);
		return $url;
	}
}